//
//  MYAlertTableView.m
//  Stock_Stategy
//
//  Created by MayerF on 2018/1/22.
//  Copyright © 2018年 MayerF. All rights reserved.
//

#import "MYAlertTableView.h"

@interface MYAlertTableView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)NSString *pTitle;
@property (nonatomic, strong)NSArray *pData;
@property (nonatomic, strong)UIView *pBgView;
@property (nonatomic, strong)UILabel *pTitleLabel;
@property (nonatomic, strong)UIButton *pCancel;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, copy)void(^clickAction)(NSUInteger row);
@end

@implementation MYAlertTableView

+ (instancetype)shared
{
    static MYAlertTableView *pAlert;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        pAlert = [[MYAlertTableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    });
    return pAlert;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0x000000, 0.3);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon {
//    UIViewController *vc = [UIViewController new];
//    self.rootViewController = vc;
    _pBgView = [UIView new];
    _pTitleLabel = [UILabel new];
    _pCancel = [UIButton new];
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.bounces = NO;
    
    _pTitleLabel.textColor = kREDColor;
    _pTitleLabel.font = [UIFont systemFontOfSize:15];
    _pTitleLabel.textAlignment = NSTextAlignmentCenter;
    _pBgView.backgroundColor = [UIColor whiteColor];
    _pCancel.titleLabel.font = [UIFont systemFontOfSize:15];
    [_pCancel setTitle:@"关闭" forState:UIControlStateNormal];
    [_pCancel setTitleColor:kREDColor forState:UIControlStateNormal];
    [_pCancel addTarget:self action:@selector(actionOnClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)initConstraints
{
    [self addSubview:_pBgView];
    [_pBgView addSubview:_pTitleLabel];
    [_pBgView addSubview:_pCancel];
    [_pBgView addSubview:_pTableView];
    
    [_pBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.width.mas_equalTo(230);
        make.height.mas_equalTo(50*4);
    }];
    [_pTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(_pBgView);
        make.bottom.mas_equalTo(_pTableView.mas_top);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(_pBgView);
        make.bottom.mas_equalTo(_pCancel.mas_top);
        make.height.mas_equalTo(50*2);
    }];
    [_pCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_pBgView);
        make.height.mas_equalTo(_pTitleLabel);
    }];
}

- (void)p_UpdateTitle:(NSString *)title data:(NSArray *)data
{
    _pTitleLabel.text = title;
    _pData = data;
    [_pTableView reloadData];
}

- (void)actionOnClick:(UIButton *)btn
{
    self.hidden = YES;
    _clickAction = nil;
}

+ (void)showTitle:(NSString *)title
             data:(NSArray<NSString *> *)data
{
    MYAlertTableView *alert = [MYAlertTableView shared];
    [alert p_UpdateTitle:title data:data];
    alert.hidden = NO;
}
+ (void)addClickAction:(void (^)(NSUInteger row))action
{
    MYAlertTableView *alert = [MYAlertTableView shared];
    alert.clickAction = action;
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _pData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.separatorColor = kMYHexToRGB(0xf6f6f6, 1);
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    NSString *mode = _pData[indexPath.row];
    static NSString *ID = @"alertTableView";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = mode;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.separatorInset = UIEdgeInsetsZero;
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _clickAction(indexPath.row);
    self.hidden = YES;
}

@end
